﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace carsitem.yuangong
{
    public partial class weihu : Form
    {
        Sunisoft.IrisSkin.SkinEngine se = null;
        DataSet ds = new DataSet();
        string skinname = "SteelBlack";
        public weihu()
        {
            InitializeComponent();
            //加载皮肤
            se = new Sunisoft.IrisSkin.SkinEngine();
            se.SkinAllForm = true;
            se.SkinFile = "skin/" + skinname + ".ssk";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
                carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
                mainInterfaceForm.Show();
                this.Hide();//把自己隐藏了
            
        }//退出功能

        private void weihu_Load(object sender, EventArgs e)
        {
            //窗口加载事件
            //创建连接对象
            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=SellCars;Integrated Security=True");
            //打开连接
            conn.Open();
            //3.书写一条查询语句
            string sql = string.Format("select * from Employee");
            //4.执行查询语句
            SqlDataAdapter dap = new SqlDataAdapter(sql, conn);
            //5.填充DataSet对象
            DataTable dl = new DataTable();
            dap.Fill(dl);
            //设置datagridview控件的数据源
            this.dataGridView1.AutoGenerateColumns = false;//取消自动生成列
            this.dataGridView1.DataSource = dl;
        }//窗体加载

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //创建连接对象
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SellCars;Integrated Security=True");
            //打开连接
            con.Open();
            //获取当行主键列的值
            int Employeeid = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
            //书写一条SQL语句
            string sql = string.Format(@"select e.EmployeeID,d.DepartName,e.EmployeeName,e.Salay,e.Sex,e.PhoneNumber,e.Password,e.Address,e.WorkName from Employee e,Department d where e.DepartID = d.DepartID and e.EmployeeID = {0}", Employeeid);
            DataTable dt = new DataTable();//在内存中创建表对象
            //执行SQL语句
            SqlDataAdapter dap = new SqlDataAdapter(sql, con);
            //4.填充DataSet对象
            dap.Fill(dt);
            this.textBox1.Text = dt.Rows[0]["EmployeeName"].ToString();//姓名
            this.numericUpDown1.Text = dt.Rows[0]["Salay"].ToString();//工资
            //性别
            if (dt.Rows[0]["Sex"].ToString()=="男")
            {
                this.radioButton1.Checked = true;
            }
            else
            {
             this.radioButton2.Checked = true;
            }
            this.textBox2.Text = dt.Rows[0]["PhoneNumber"].ToString();//电话
            this.comboBox1.Text = dt.Rows[0]["DepartName"].ToString();//部门
            this.textBox5.Text = dt.Rows[0]["Password"].ToString();//密码
            this.textBox4.Text = dt.Rows[0]["Address"].ToString();//住址
            this.textBox6.Text = dt.Rows[0]["WorkName"].ToString();//职位
        }//显示信息

        private void button2_Click(object sender, EventArgs e)
        {
            //删除功能
            //创建连接对象
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SellCars;Integrated Security=True");
            //打开连接
            con.Open();
            //获取当前主键列的值
            int Employeeid = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
            string sql = string.Format(@"delete from Employee where EmployeeID='{0}'", Employeeid);
            SqlCommand com = new SqlCommand(sql, con);
            int count = com.ExecuteNonQuery();
            if (count>0)
            {
                MessageBox.Show("删除成功");
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            string sqll = string.Format("select * from employee");
            //4.执行查询语句
            SqlDataAdapter dap = new SqlDataAdapter(sqll, con);
            //5.填充dataset对象
            DataTable dl = new DataTable();
            dap.Fill(dl);
            //设置datagridview控件的数据源
            this.dataGridView1.AutoGenerateColumns = false;//取消自动生成列
            this.dataGridView1.DataSource = dl;
        }//删除功能

        private void button1_Click(object sender, EventArgs e)
        {
            //创建连接对象
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SellCars;Integrated Security=True");
            //打开连接
            con.Open();
            //获取当前主键列的值
            int Employeeid = Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value);
            //接收用户输入的值
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("员工姓名不能为空");
                return;
            }
            string name = this.textBox1.Text;//姓名
            if (this.comboBox1.Text == "")
            {
                MessageBox.Show("部门不能为空");
                return;
            }
            int st = 0;
            string bumeng = this.comboBox1.Text;//部门
            string bm1 = string.Format(@"select DepartID from Department where DepartName='{0}'", bumeng);
            DBHelper dh = new DBHelper();
            st = Convert.ToInt32(dh.GetDataTableBySql(bm1).Rows[0]["DepartID"].ToString());
            if (this.numericUpDown1.Text == "")
            {
                MessageBox.Show("工资不能为空");
                return;
            }
            string gongzi = this.numericUpDown1.Text;//工资

            if (this.textBox2.Text == "")
            {
                MessageBox.Show("手机不能为空");
                
            }
            string shouji = this.textBox2.Text;//手机
            IsNumberic(shouji);
            if (this.textBox2.Text.Length != 11)
            {
                MessageBox.Show("手机号码为11位！");
            }
            else
            {
                if ((this.radioButton1.Checked == false) && (this.radioButton2.Checked == false))
                {
                    MessageBox.Show("请选择性别");
                    return;
                }
                string xingb = "";//性别
                if (this.radioButton1.Checked == true)
                {
                    xingb = "男";
                }
                if (this.radioButton2.Checked == true)
                {
                    xingb = "女";
                }
                if (this.textBox5.Text == "")
                {
                    MessageBox.Show("密码不能为空");
                    return;
                }
                string mima = this.textBox5.Text;//密码
                string zhuzhi = this.textBox4.Text;//住址
                string zhiwei = this.textBox6.Text;//职位
                                                   //书写一条SQL语句
                string sql = string.Format(@"update Employee set DepartID='{0}',EmployeeName='{1}',Salay='{2}',sex='{3}',PhoneNumber='{4}',Password='{5}',Address='{6}',WorkName='{7}' where EmployeeID={8}", st, name, gongzi, xingb, shouji, mima, zhuzhi, zhiwei, Employeeid);
                //执行SQL语句
                SqlCommand com = new SqlCommand(sql, con);
                int count = com.ExecuteNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
                string sqll = string.Format("select * from Employee");
                //4.执行查询语句
                SqlDataAdapter dap = new SqlDataAdapter(sqll, con);
                //5.填充DataSet对象
                DataTable dl = new DataTable();
                dap.Fill(dl);
                //设置datagridview控件的数据源
                this.dataGridView1.AutoGenerateColumns = false;//取消自动生成列
                this.dataGridView1.DataSource = dl;
            }
            
        }//修改功能

        private void weihu_FormClosed(object sender, FormClosedEventArgs e)
        {
            carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
            mainInterfaceForm.Show();
            this.Hide();//把自己隐藏了
        }
        //数字认证
        public bool IsNumberic(string oText)
        {
            string pattern = @"^\d+(\.\d)?$";
            if (oText != "")
            {
                if (!Regex.IsMatch(oText, pattern))
                    MessageBox.Show("请输入数字！");
            }
            return true;
        }
    }
}
